package com.saicent.note.controller;

import java.io.File;
import java.util.List;

import org.springframework.web.bind.annotation.ModelAttribute;
import com.saicent.note.core.ArticleManager;
import com.saicent.note.core.Category;
import com.saicent.support.web.mvc.controller.spring.AnnotationControllerSupport;

public class AbstractController extends AnnotationControllerSupport {

	private static final String SESSION_KEY_CURRENT_CATEGORY = "currentCategory";
	protected static final Category DEFAULT_CATEGORY = new Category("all", "所有文章");
	protected static final String JSP_BASE_LOCATION = File.separator + "WEB-INF" + File.separator + "view" + File.separator;
	
	@ModelAttribute("categories")
	public List<Category> allCategories() {
		return getArticleManager().allCategories();
	}
	
	protected ArticleManager getArticleManager() {
		return contextAttribute(ArticleManager.class.getName());
	}
	
	public Category currentCategory() {
		return (Category) getSession().getAttribute(SESSION_KEY_CURRENT_CATEGORY);
	}
	
	public void currentCategory(Category category) {
		getSession().setAttribute(SESSION_KEY_CURRENT_CATEGORY, category);
	}
	
	public void resetCurrentCategory() {
		getSession().setAttribute(SESSION_KEY_CURRENT_CATEGORY, DEFAULT_CATEGORY);
	}
}
